#include "goodbyeTrick.h"


//--------------------------------------------------------------
void goodbyeTrick::setup(){
	sprintf(name, "goodbye trick");
	
	bTakeSnapshot = false;
	bHaveBlobs = false;
	meAsGray.allocate(320,240);
	meAndedWithHand.allocate(320,240);
	bUncovered = false;
	bShow = false;
	
	energy = 0;
	phase = 0;
	phaseAdder = 0;
	energyTarget = 0;
	
}


//--------------------------------------------------------------
void goodbyeTrick::reset(){
	bHaveBlobs = false;
	bUncovered = false;
	bShow = false;
	meAndedWithHand.set(0);
	meAsGray.set(0);
	
	
	energy = 0;
	phase = 0;
	phaseAdder = 0;
	energyTarget = 0;
}

//--------------------------------------------------------------
void goodbyeTrick::update(){
	
	
	energy = 0.98f * energy + 0.02f * energyTarget;
	phaseAdder = 0.5f * energy;
	phase += phaseAdder;
	
	if (bTakeSnapshot == true){
		vbd->takeSnapshot();
		if (vbd->nBlobs > 0){
			memcpy(&blob,&vbd->vidBlob, sizeof(videoBlob));
			bHaveBlobs = true;
		}
		bTakeSnapshot = false;
		bUncovered = false;
		bShow = true;
	}
	
	
	if (bHaveBlobs == true){
		//blob.angle = ofRandom(0,TWO_PI);
		blob.pos.x += (3.5f*powf(energy,1.5f)) * sin(phase*0.3);
		blob.angle += (0.02f*powf(energy,0.5f)) * sin(phase*0.7f);
		
		blob.update();
	}
	
}

//--------------------------------------------------------------
void goodbyeTrick::draw(){
	if (bHaveBlobs == true){
		if (bShow == true){
			
			ofEnableAlphaBlending();
			ofSetColor(255,255,255,energy * 255);
			
			font.drawString("goodbye! thank you! gracias!\n", 200,100);
			
			
			blob.draw();
		    ofDisableAlphaBlending();
			 
		} else {
			ofEnableAlphaBlending();
			ofSetColor(255,0,0,155);
			if (bDrawDiagnostically) blob.drawOutline();
			ofDisableAlphaBlending();
		}
	}
	
	ofSetColor(255,255,255);
	//meAsGray.draw(0,0);	
	//meAndedWithHand.draw(0,240);
}


//--------------------------------------------------------------
void goodbyeTrick::keyPressed(int key){
	if (key == 's'){
		bTakeSnapshot = true;
	} else if (key == ' '){
		reset();
	} else if (key == '1'){
		energyTarget = 1;
	} else if (key == '2'){
		energyTarget = 0;
	}
}